$(document).ready(function() {	
	$('.tipsy').tipsy({gravity: 'w'});// nw | n | ne | w | e | sw | s | se --- http://onehackoranother.com/projects/jquery/tipsy/	
	$("#frmCreate").validate({
		errorClass		: 'errorMessage',
		errorElement	: 'span',
		onfocusout		: false,
		onkeyup			: false,
		rules: {
			pcategoryid:{
				min						: 1
			},				
			pname:{
				required				: true,
				minlength				: 5
			},
			pimage: {
				required				: true,
				validateImgFormat		: true,
				validateImgSize			: true
			},			
			pcontent: {
				validateContentRequired	: true,
				validateContentMinlength: true
			}
		},
		messages: {	
			pcategoryid:{
				min						: cate_required
			},	
			pname:{
				required				: name_required,
				minlength				: name_minlength
			},
			pimage: {
				required				: image_required,
				validateImgFormat		: image_format,
				validateImgSize			: image_size
			},			
			pcontent: {
				validateContentRequired	: content_required,
				validateContentMinlength: content_minlength
			}
		},
		errorPlacement: function(label, element) {
			var id = element.attr('id');	
			if(id == "pimage") {
				label.insertAfter($('#simage'));					
			} else if(id == "pcategoryid") {
				label.insertAfter($('#selcate'));					
			}else if(id == "pcontent") {			
				label.insertAfter($('#pcontent___Frame'));
			} else {
				label.insertAfter($('#' + id));			
			}
			
		}				 
	});
	$.validator.addMethod('validateImgFormat', function() {
		var strLinkImage = $('#pimage').val();
		if(strLinkImage == "") {
			return false;
		}
		return validateImage(strLinkImage);
	});
	$.validator.addMethod('validateImgSize', function() {
		var strLinkImage = $('#pimage').val();
		if(strLinkImage == "") {
			return false;
		}
		return validateImgSize("pimage");
	});	
	$.validator.addMethod('validateContentRequired',function(){
		var oEditorContent 	= FCKeditorAPI.GetInstance('pcontent') ;
		var strContent		= stripTags(oEditorContent.GetXHTML( true ), 1);		
		if(strContent == '')
			return false;		
		return true;												 
	});
	$.validator.addMethod('validateContentMinlength',function(){
		var oEditorContent 	= FCKeditorAPI.GetInstance('pcontent') ;
		var strContent		= stripTags(oEditorContent.GetXHTML( true ), 1);		
		if(strContent != '' && strContent.length < 20)
			return false;		
		return true;												 
	});	
});
loadSub = function(intValue, strDiv) {
	$.getJSON(
		HOST_VFRIEND + 'ajax/subcate?type=10&parentid=' + intValue + '&callback=?',		
		function(response) {										
			$('#'+strDiv).html(response['data']);
		}
	);
}
function lookup(inputString) {
	if(inputString.length == 0 ) {		
		$('#suggestions').hide();
	} else {	
		$('#suggestions').show();
		$('#autoSuggestionsList').html('<p align="center"><img src="'+ HOST_STATICS+'img/bg/loading-bar.gif" alt="diem tin,ket ban,tim ban" width="88" height="7" /><br/>Bạn vui lòng chờ trong giây lát.<br/></p>');
		$.getJSON(
			HOST_VFRIEND + 'ajax/smartsearchnews?val=' + encodeURIComponent(inputString) + '&callback=?',		
			function(response) {				
				if(response['data'] != '') {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(response['data']);
				}
				else {
					$('#suggestions').hide();
				}
			}
		);
	}
} // lookup

function selectItem(thisValue,thisHidden) {
	$('#pname').val(thisValue);
	$('#pnexistid').val(thisHidden);
	setTimeout("$('#suggestions').hide();", 200);
}

function hideLookup() {
	$('#suggestions').hide();
}